App.require(["canvasTool"], function (canvasTool) {
    function Resource() {
        this.resources = [];
        this.resourceMap = {};
    }

    Resource.prototype = {
        constructor: Resource,
        destroy: function () {
            this.resources.length = 0;
            this.resources = null;
            this.resourceMap = null;
        },
        addImage: function (src) {
            if (this.resources.indexOf(src) == -1 && !this.resourceMap[src]) this.resources.push(src);
        },
        loadComplete: function (bk) {
            var resources = this.resources,
                len = resources.length,
                resourceMap = this.resourceMap;

            if (len === 0) return bk();
            var doFn = function (src) {
                return function () {
                    resources.splice(resources.indexOf(src), 1);
                    if (--len == 0) {
                        if (typeof bk === "function") bk();
                    }
                }
            }
            resources.forEach(function (url, i) {
                var img = new Image();
                img.src = url;
                resourceMap[url] = img;
                img.onload = doFn(url);
            })
        },
        isComplete: function () {
            return this.resources.length == 0;
        },
        getImage: function (url) {
            return this.resourceMap[url];
        }
    };

    App.define("resource", {
        resource: Resource
    })
});